﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;

namespace MultiThreading
{
    public interface IJob
    {
        void Execute();
        object[] GetExecuteStatus();
    }

    public class Job : IJob
    {
        private static int _Step=10;
        private static ConcurrentDictionary<int, bool> _JobProcess;

        public void Execute()
        {
            if (_JobProcess == null || _JobProcess.Count== 0||_JobProcess.Count>0)
            {
                _JobProcess = new ConcurrentDictionary<int, bool>();
                for (int i = 1; i <= _Step; i++)
                {
                    _JobProcess.TryAdd(i, false);
                }
            }
            for (int i = 1; i <= _Step; i++)
            {
                _JobProcess[i] = true;
                Thread.Sleep(2000);
            }
        }

        public object[] GetExecuteStatus()
        {
            object[] status = null;
            if (_JobProcess != null)
            {
                int completedCount = (from item in _JobProcess
                                                   where item.Value == true
                                                   select item).Count();
                int totalCount = (from item in _JobProcess                                 
                                  select item).Count();

                int itemInProcess = (from item in _JobProcess
                                        where item.Value == false
                                        orderby item.Key
                                        select item.Key).FirstOrDefault();

                status = new object[] { totalCount, completedCount, itemInProcess };
            }
            return status;
        }
    }
}
